<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getpageList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getpageList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="$router.push('/goods/add')"
            >添加商品</el-button
          >
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#" width="50"></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="700"
        ></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="120"
        ></el-table-column>
        <el-table-column label="创建时间">
          <template v-slot="scope">{{
            scope.row.add_time | dataFormate('YYYY-MM-DD HH:mm:ss')
          }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            {{ scope.row.goods_id }}
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="showGoodsDialog(scope.row)"
            ></el-button>
            <el-button
              size="mini"
              icon="el-icon-delete"
              type="danger"
              @click="delGoodsById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

      <!-- 修改商品对话框 -->
      <el-dialog
        title="修改商品"
        :visible.sync="editGoodsDialogVisible"
        width="50%"
        @close="editGoodsClosed"
      >
        <el-form
          :model="editForm"
          :rules="editFormRules"
          ref="editFormRef"
          label-width="80px"
        >
          <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="editForm.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model.number="editForm.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品重量" prop="goods_weight">
            <el-input v-model.number="editForm.goods_weight"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number">
            <el-input v-model.number="editForm.goods_number"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editGoodsDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editGoods">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取商品列表的查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表的数组
      goodsList: [],
      // 修改商品列表对话框的布尔值
      editGoodsDialogVisible: false,
      // 商品的总数
      total: 0,
      // 修改商品列表的配置项
      editForm: {},
      // 修改商品列表的校验规则
      editFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          {
            required: true,
            message: '请输入商品价格',
            trigger: 'blur',
            type: 'number'
          }
        ],
        goods_weight: [
          {
            required: true,
            message: '请输入商品重量',
            trigger: 'blur',
            type: 'number'
          }
        ],
        goods_number: [
          {
            required: true,
            message: '请输入商品数量',
            trigger: 'blur',
            type: 'number'
          }
        ]
      }
    }
  },
  methods: {
    getpageList() {
      this.queryInfo.pagenum = 1
      this.getGoodsList()
    },
    // 获取商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get(`goods`, {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表信息失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 修改每页显示多少条  pagesize改变触发的函数
    handleSizeChange(newVal) {
      // newVal是最新的pagesize值,更新给pagesize
      this.queryInfo.pagesize = newVal
      // 重新发送请求
      this.getGoodsList()
    },
    // 分页pagenum发生变化触发的方法
    handleCurrentChange(newVal) {
      // newVal是最新的pagenum值,更新给pagenum
      this.queryInfo.pagenum = newVal
      // 重新发送请求
      this.getGoodsList()
    },
    // 删除商品
    async delGoodsById(id) {
      // 提示对话框
      const res = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // confirm 表示点击的是确认按钮
      // cancel  表示点击的是取消按钮
      if (res != 'confirm') {
        return this.$message.info('已取消删除')
      }
      // 发送删除的请求
      const { data: result } = await this.$http.delete('goods/' + id)
      console.log(result)
      if (result.meta.status !== 200) {
        return this.$message.error('删除商品失败,请稍后重试')
      }
      this.$message.success('删除商品成功')
      // 重新加载数据
      this.getGoodsList()
    },
    // 重置修改商品表单数据
    editGoodsClosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 点击编辑按钮显示表单数据
    async showGoodsDialog(row) {
      // console.log(row);
      const { data: res } = await this.$http.get(`goods/${row.goods_id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      console.log(res)
      this.editForm = res.data

      this.editGoodsDialogVisible = true
    },
    // 修改商品列表
    editGoods() {
      // 预校验
      this.$refs.editFormRef.validate(async boo => {
        if (!boo) {
          return this.$message.error('请输入必填信息')
        }
        const { data: res } = await this.$http.put(
          `goods/${this.editForm.goods_id}`,
          {
            goods_name: this.editForm.goods_name,
            goods_price: this.editForm.goods_price,
            goods_weight: this.editForm.goods_weight,
            goods_number: this.editForm.goods_number
          }
        )
        console.log(res)

        if (res.meta.status !== 201) {
          return this.$message.error('更新失败')
        }
        this.$message.success('更新成功')
        // 调用商品分类的方法
        this.getGoodsList()

        // 关闭对话框
        this.editGoodsDialogVisible = false
      })
    }
  },
  created() {
    // 调用商品列表的方法
    this.getGoodsList()
  }
}
</script>
<style lang="less" scoped></style>
